package demo1.web.servlet.userMaiapi;

import demo1.dao.Mapper.goodsMapper;
import demo1.pojo.goods;
import demo1.pojo.userMai;
import demo1.dao.sql.goodssql;
import demo1.service.userMaiService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Objects;

@WebServlet("/modifyGoodsStatue2")
public class modifyGoodsStatue2 extends HttpServlet {//修改商品的状态
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获取session对
        HttpSession session = req.getSession();
        Object userObject = session.getAttribute("users");
        userMai users = (userMai) userObject;

        String statue = req.getParameter("typeSelection");
        int goodsID = Integer.parseInt(req.getParameter("goodsID"));
        try {
            if(Objects.equals(statue, "上架")){
                userMaiService check = new userMaiService();
                if(statue.equals(check.checkGoodsStatue(users.getId(), goodsID))){//如果同时都是上架，不执行
                    resp.sendRedirect("/logindemo2/failModifyUpStatue.jsp");
                }
                else {
                    int num = check.checkGoodsNum(goodsID);//查看库存是不是为0
                    if(num == 0){
                        //没有库存，不上架
                        resp.sendRedirect("/logindemo2/failModifyGoods.jsp");
                    }
                    else {
                        //如果有库存
                        userMaiService update = new userMaiService();
                        update.updateGoodsTime(users.getId(), goodsID);//更新上架的时间
                        update.updateGoodsStatue(statue, users.getId(), goodsID);//更新商品的状态
                        req.getRequestDispatcher("showgoods").forward(req,resp);//转发到另一个界面
                    }
                }

            }
            else {//如果是下架
                userMaiService update = new userMaiService();
                if(statue.equals(update.checkGoodsStatue(users.getId(), goodsID))){//如果同时都是下架，不执行
                    resp.sendRedirect("/logindemo2/failModifyDownStatue.jsp");
                }
                else {
                    update.updateGoodsStatue(statue, users.getId(), goodsID);//更新商品的状态
                    req.getRequestDispatcher("showgoods").forward(req,resp);//转发到另一个界面
                }

            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
